package com.ziyun.pop.web.modules.quoteFastPrintMaterial.controller;

import com.utility.JsonUtils;
import com.utility.StringUtils;
import com.utility.http.HttpUtils;
import com.ziyun.pop.common.utils.UserUtils;
import com.ziyun.pop.modules.quoteFactoryMaterial.entity.QuoteFactoryMaterialEntity;
import com.ziyun.pop.modules.quoteFastPrintMaterial.entity.QuoteFastPrintMaterialEntity;
import io.renren.common.annotation.SysLog;
import io.renren.common.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.jws.soap.SOAPBinding;
import java.math.BigDecimal;
import java.util.*;


/**
 * 快印材料
 *
 * @author llc
 * @email 
 * @date 2018-09-19 09:04:01
 */
@RestController
@RequestMapping("quoteFastPrintMaterial/quotefastprintmaterial/")
public class QuoteFastPrintMaterialController {

    @Value("${erpApi}")
    private String erpApi ;

    /*
     * 列表
     */
    @RequestMapping("/list")
    /*@RequiresPermissions("quoteFastPrintMaterial:quotefastprintmaterial:list")*/
    public R list(@RequestParam Map<String, Object> params){
        // 获取当前用户的商户id
        Long sellerId =  UserUtils.getCurentUser().getSellerId();
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("sellerId", sellerId);
        String url = erpApi + "/quoteFastPrintMaterial/quotefastprintmaterial/list";
        String response =  HttpUtils.doPost(url,paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
    }

    /**
     * 查看详情
     */

    @RequestMapping("/detail/{optionId}")
   /* @RequiresPermissions("quoteFactoryMaterial:quotefactorymaterial:info")*/
    public R detail(@PathVariable("optionId") Integer optionId){
        // 获取当前用户的商户id
        Long sellerId =  UserUtils.getCurentUser().getSellerId();
        //封装查询参数
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("sellerId", sellerId);
        paramBodyMap.put("optionId", optionId);
        String url = erpApi + "/quoteFastPrintMaterial/quotefastprintmaterial/detail";
        String response =  HttpUtils.doPost(url,paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
    }

   /**
     * 查看所有材料
     */
    @RequestMapping("/findAllMaterial")
    /*@RequiresPermissions("quoteFastPrintMaterial:quotefastprintmaterial:list")*/
    public R findAllMaterial(){
        String url = erpApi + "/quoteFastPrintMaterial/quotefastprintmaterial/findAllMaterial";
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        String response =  HttpUtils.doPost(url,paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
    }

    /**
     * 查看所有optionName2(克重)
     */
    @RequestMapping("/findAllOptionName2")
   /* @RequiresPermissions("quoteFastPrintMaterial:quotefastprintmaterial:list")*/
    public R findAllOptionName2(@RequestBody QuoteFastPrintMaterialEntity quoteFastPrintMaterial){
        String optionName=quoteFastPrintMaterial.getOptionName();
        String url = erpApi + "/quoteFastPrintMaterial/quotefastprintmaterial/findAllOptionName2";
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("optionName",optionName);
        String response =  HttpUtils.doPost(url,paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
    }

     /**
     * 保存
     */
    @SysLog("保存快印材料")
    @RequestMapping("/save/{ids}")
    @Transactional(rollbackFor = Exception.class)
    /*@RequiresPermissions("quoteFastPrintMaterial:quotefastprintmaterial:save")*/
    public R save(@PathVariable("ids") String ids){
        List<QuoteFactoryMaterialEntity>     quoteFactoryMaterialList=new ArrayList<QuoteFactoryMaterialEntity>();
       if(StringUtils.isNotBlank(ids)){
           // 获取当前用户的商户id
           Long sellerId =  UserUtils.getCurentUser().getSellerId();
           String[] idArray=ids.split(",");
           for (int i=0;i<idArray.length;i++){
               QuoteFactoryMaterialEntity quoteFastPrintMaterial=new QuoteFactoryMaterialEntity();
               quoteFastPrintMaterial.setFactoryId(sellerId.intValue());
               quoteFastPrintMaterial.setOptionId(Integer.parseInt(idArray[i]));
               quoteFastPrintMaterial.setMaterialPrice(new BigDecimal(0));
               quoteFastPrintMaterial.setCreateDate(new Date());
               quoteFastPrintMaterial.setCreateUser(UserUtils.getCurentUser().getUsername());
               quoteFastPrintMaterial.setUpdateDate(new Date());
               quoteFastPrintMaterial.setIsSpecialtyPaper(0);
               quoteFastPrintMaterial.setMaterialType("shumakuaiyin");
               quoteFastPrintMaterial.setUpdateUser(UserUtils.getCurentUser().getUsername());
               quoteFactoryMaterialList.add(quoteFastPrintMaterial);
           }
       }
        String url = erpApi + "/quoteFastPrintMaterial/quotefastprintmaterial/save";
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
//        paramBodyMap.put("sellerId",UserUtils.getCurentUser().getSellerId());
        paramBodyMap.put("quoteFactoryMaterialList",JsonUtils.object2String(quoteFactoryMaterialList));
        String response =  HttpUtils.doPost(url,paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
    }

     /**
     * 修改
     */
     @SysLog("修改快印材料")
    @RequestMapping("/update/{ids}")
    @Transactional(rollbackFor = Exception.class)
   /* @RequiresPermissions("quoteFastPrintMaterial:quotefastprintmaterial:update")*/
    public R update(@PathVariable("ids") String ids){
        // 获取当前用户的商户id
        Long sellerId =  UserUtils.getCurentUser().getSellerId();
        String url = erpApi + "/quoteFastPrintMaterial/quotefastprintmaterial/update";
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("sellerId",sellerId);
        paramBodyMap.put("ids",ids);
        paramBodyMap.put("updateUser",UserUtils.getCurentUser().getUsername());
        String response =  HttpUtils.doPost(url,paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
    }


    /**
     * 查看所有快印打印机
     */
    @RequestMapping("/printList")
    /*@RequiresPermissions("quoteFastPrintMaterial:quotefastprintmaterial:list")*/
    public R printList(){
        // 获取当前用户的商户id
        Long sellerId =  UserUtils.getCurentUser().getSellerId();
        String url = erpApi + "/quoteFastPrintMaterial/quotefastprintmaterial/printList";
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("sellerId",sellerId);
        String response =  HttpUtils.doPost(url,paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
    }


    /**
     * 生成组合
     */
    @SysLog("生成组合报价")
    @RequestMapping("/generateGroup")
    /*@RequiresPermissions("quoteFastPrintMaterial:quotefastprintmaterial:save")*/
    public R generateGroup(@RequestBody List<Object> quoteFastPrintGroupPrice){
        // 获取当前用户的商户id
        Long sellerId =  UserUtils.getCurentUser().getSellerId();
        String url = erpApi + "/quoteFastPrintMaterial/quotefastprintmaterial/generateGroup";
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("quoteFactoryMaterialList",JsonUtils.object2String(quoteFastPrintGroupPrice));
        paramBodyMap.put("updateUser",UserUtils.getCurentUser().getUsername());
        paramBodyMap.put("sellerId",sellerId);
        String response =  HttpUtils.doPost(url,paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
    }






     /**
     * 获取对应的信息和已勾选的信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("quoteFastPrintMaterial:quotefastprintmaterial:info")
    public R info(@PathVariable("id") Integer id){
        // 获取当前用户的商户id
        Long sellerId =  UserUtils.getCurentUser().getSellerId();
        String url = erpApi + "/quoteFastPrintMaterial/quotefastprintmaterial/info";
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("sellerId",sellerId);
        paramBodyMap.put("id",id);
        String response =  HttpUtils.doPost(url,paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
    }

    /**
     * 删除
     */
    @SysLog("删除快印材料")
    @RequestMapping("/delete")
    @RequiresPermissions("quoteFastPrintMaterial:quotefastprintmaterial:delete")
    public R delete(@RequestBody Integer[] optionIds){
        // 获取当前用户的商户id
        Long sellerId =  UserUtils.getCurentUser().getSellerId();
        String url = erpApi + "/quoteFastPrintMaterial/quotefastprintmaterial/delete";
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("sellerId",sellerId);
        paramBodyMap.put("optionIds",JsonUtils.object2String(optionIds));
        paramBodyMap.put("updateUser",UserUtils.getCurentUser().getUsername());
        String response =  HttpUtils.doPost(url,paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
    }

    /**
     * 修改是否启用
     */
    @SysLog("快印材料是否启用")
    @RequestMapping("/updateDisplay/{id}")
    @RequiresPermissions("quoteFastPrintMaterial:quotefastprintmaterial:info")
    public R updateDisplay(@PathVariable("id") Integer id) {
        String url = erpApi + "/quoteFastPrintMaterial/quotefastprintmaterial/updateDisplay";
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("id",id);
        paramBodyMap.put("updateUser",UserUtils.getCurentUser().getUsername());
        String response =  HttpUtils.doPost(url,paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
    }
}
